package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.shop.domain.bo.ErpSellerStoreSubcategoryBo;
import com.erp.shop.domain.vo.ErpSellerStoreSubcategoryVo;
import com.erp.shop.mapper.ErpSellerStoreSubcategoryMapper;
import com.erp.shop.service.IErpSellerStoreSubcategoryService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.gather.api.RemoteProductCategoryBrandService;
import com.ruoyi.shop.api.domain.ErpSellerStoreSubcategory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 卖家店铺所在类目Service业务层处理
 *
 * @author erp
 * @date 2023-05-09
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreSubcategoryServiceImpl implements IErpSellerStoreSubcategoryService {

//    @Autowired
//    RestTemplate restTemplate;
//    private final AEConfig aeConfig;

    private final ErpSellerStoreSubcategoryMapper baseMapper;
    private final RemoteProductCategoryBrandService categoryBrandService;

    /**
     * 查询卖家店铺所在类目
     */
    @Override
    public ErpSellerStoreSubcategoryVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询卖家店铺所在类目列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreSubcategoryVo> queryPageList(ErpSellerStoreSubcategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreSubcategory> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreSubcategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询卖家店铺所在类目列表
     */
    @Override
    public List<ErpSellerStoreSubcategoryVo> queryList(ErpSellerStoreSubcategoryBo bo) {
        LambdaQueryWrapper<ErpSellerStoreSubcategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreSubcategory> buildQueryWrapper(ErpSellerStoreSubcategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreSubcategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ErpSellerStoreSubcategory::getUserId, bo.getUserId());
        lqw.eq(bo.getSellerStoreId() != null, ErpSellerStoreSubcategory::getSellerStoreId, bo.getSellerStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getCategoryIds()), ErpSellerStoreSubcategory::getCategoryIds, bo.getCategoryIds());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreSubcategory::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增卖家店铺所在类目
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreSubcategoryBo bo) {
        ErpSellerStoreSubcategory add = BeanUtil.toBean(bo, ErpSellerStoreSubcategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改卖家店铺所在类目
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreSubcategoryBo bo) {
        ErpSellerStoreSubcategory update = BeanUtil.toBean(bo, ErpSellerStoreSubcategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreSubcategory entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除卖家店铺所在类目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<String> queryStoreSubCategory(Long sellerId) {
//        Long userId = LoginHelper.getUserId();
        ErpSellerStoreSubcategory erpSellerStoreSubcategory = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStoreSubcategory>()
                        .eq(ErpSellerStoreSubcategory::getSellerStoreId, sellerId)

        );
        if (erpSellerStoreSubcategory != null) {
            return R.ok( erpSellerStoreSubcategory.getCategoryIds(),"查找成功");
        } else {
            return R.ok();
        }
    }

    @Override
    public R<Object> querySubCategoryListByStoreId(Long storeId) {
//        Long userId = SecurityUtils.getUserId();
        ErpSellerStoreSubcategory erpSellerStoreSubcategory = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStoreSubcategory>()
                        .eq(ErpSellerStoreSubcategory::getSellerStoreId, storeId)
                        .last("limit 1")

        );
        if (erpSellerStoreSubcategory != null) {
            R<Object> categoryList = categoryBrandService.getCategoryList(erpSellerStoreSubcategory.getCategoryIds());
            return categoryList;
        } else {
            return R.ok("");
        }
    }

    @Override
    public R<Object> getSubcategoryByStoreIdAndCategoryId(Long storeId, Long categoryId) {
        ErpSellerStoreSubcategory erpSellerStoreSubcategory = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStoreSubcategory>()
                .eq(ErpSellerStoreSubcategory::getSellerStoreId, storeId)
                .like(ErpSellerStoreSubcategory::getCategoryIds, categoryId)
                .last("limit 1"));
        if (Objects.isNull(erpSellerStoreSubcategory)){
            return R.fail("未查找到信息");
        }else {
            return R.ok(erpSellerStoreSubcategory);
        }
    }

    @Override
    public R<String> chooseStoreSubCategory(ErpSellerStoreSubcategoryBo bo) {
        //1、查看当前该店铺是否已经有了数据
        ErpSellerStoreSubcategory erpSellerStoreSubcategory = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStoreSubcategory>()
                        .eq(ErpSellerStoreSubcategory::getSellerStoreId, bo.getSellerStoreId())
                        .eq(ErpSellerStoreSubcategory::getUserId, bo.getUserId())
        );
        if (erpSellerStoreSubcategory != null) {
            erpSellerStoreSubcategory.setCategoryIds(bo.getCategoryIds());
            baseMapper.updateById(erpSellerStoreSubcategory);
            return R.ok("更新成功");
        } else {
            erpSellerStoreSubcategory = new ErpSellerStoreSubcategory();
            erpSellerStoreSubcategory.setSellerStoreId(bo.getSellerStoreId());
            erpSellerStoreSubcategory.setUserId(bo.getUserId());
            erpSellerStoreSubcategory.setCategoryIds(bo.getCategoryIds());
            baseMapper.insert(erpSellerStoreSubcategory);
            return R.ok("选择成功");
        }
    }


    @Override
    public R<Object> getChildAttributesResultByPostCateIdAndPath(String param1, Long storeId) {
//        ResponseEntity<String> forEntity = restTemplate
//                .getForEntity(aeConfig.getEndpoint() + "/erp/erpProduct/getchildattributesresultbypostcateidandpath?param1=" + param1 + "&sellerId=" + storeId, String.class);
//        String body = (String) forEntity.getBody();
//        return R.ok(body);
        return R.ok();
    }
}
